﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeberTimeTracker.Models;

namespace WeberTimeTracker.Controllers
{
    public class HomeController : Controller
    {

        //
        // GET: /Home/

        public ActionResult Index()
        {
            if (Session["pop"] == null){
            Session["pop"] = false;//Session variable for disabling controls in WorkOnProject
            }                      //also determines whether the popup is needed or not 

            using (var data =  new TimeTrackerContext())
			{
                var result = data.Times.ToList();
                return View(result);
			}
		}

        public ActionResult EnterTime()//made some dropdown lists with this
        {
            var data = new TimeTrackerContext();
            var query1 = data.Users.Select(c => new { c.User_ID, c.First_Name });
            ViewBag.UserId = new SelectList(query1.AsEnumerable(), "User_ID", "First_Name");//just using first name for now
            var query2 = data.Projects.Select(c => new { c.Project_ID, c.Project_Name });
            ViewBag.ProjectId = new SelectList(query2.AsEnumerable(), "Project_ID", "Project_Name");
            return PartialView(new Time());
            
        }
        
        [HttpPost]
        public ActionResult EnterTime(Time time)
        {
            var data = new TimeTrackerContext();
            if (ModelState.IsValid)
            {
                data.Times.Add(time);
                data.SaveChanges();
                return RedirectToAction("Index");
            }
            
            var query1 = data.Users.Select(c => new { c.User_ID, c.First_Name });
            ViewBag.UserId = new SelectList(query1.AsEnumerable(), "User_ID", "First_Name");
            var query2 = data.Projects.Select(c => new { c.Project_ID, c.Project_Name });
            ViewBag.ProjectId = new SelectList(query2.AsEnumerable(), "Project_ID", "Project_Name");
            return PartialView(new Time());

        }

        public ActionResult Popup()
        {
            var data = new TimeTrackerContext();

            if (Session["Project"] == null)//default values if there is no project data available
            {
                Session["Project"]=9;
                Session["User"]=1;
            }
            
            int projectid = (int)Session["Project"];

            if (Session["PopupTimeRecord"] == null)
            {
                Session["PopupTimeRecord"] = 0;
            }

            Session["PopupTimeRecord"] = (int)Session["PopupTimeRecord"] + 5;//adding five minutes everytime the popup comes up

            var query1 = data.Projects.Where(c => c.Project_ID == projectid);
			return PartialView(query1);
			
        }
        
        [HttpPost]
        public ActionResult Popup(bool? done)
        {
            var data = new TimeTrackerContext();
            int projectid = (int)Session["Project"];
			var time = new Time();
            time.User_ID = (int)Session["User"];
            time.Project_ID = (int)Session["Project"];
            time.Hours = decimal.Round(Convert.ToDecimal((int)Session["PopupTimeRecord"])/60, 2);
            time.Date = DateTime.Now;
            time.Description = "This time was entered passively.";
            if (ModelState.IsValid)
            {
                //data.Times.Add(time); //don't want changes to be made yet
                //data.SaveChanges();
                Session["pop"] = done;
                Session["PopupTimeRecord"]=0;
                var query1 = data.Projects.Where(c => c.Project_ID == projectid);
				return PartialView(query1);
            }
            var query2 = data.Projects.Where(c => c.Project_ID == projectid);
            return PartialView(query2);
        }

        public ActionResult WorkOnProject()
        {
            var data = new TimeTrackerContext();
            var query1 = data.Users.Select(c => new { c.User_ID, c.First_Name });
            ViewBag.UserId = new SelectList(query1.AsEnumerable(), "User_ID", "First_Name");
            var query2 = data.Projects.Select(c => new { c.Project_ID, c.Project_Name });
            ViewBag.ProjectId = new SelectList(query2.AsEnumerable(), "Project_ID", "Project_Name");
            return PartialView();

        }

        [HttpPost]
        public ActionResult WorkOnProject(Time time)
        {
			var data = new TimeTrackerContext();
            
            var query1 = data.Users.Select(c => new { c.User_ID, c.First_Name });
			ViewBag.UserId = new SelectList(query1.AsEnumerable(), "User_ID", "First_Name");
			var query2 = data.Projects.Select(c => new { c.Project_ID, c.Project_Name });
			ViewBag.ProjectId = new SelectList(query2.AsEnumerable(), "Project_ID", "Project_Name");

			if (ModelState.IsValid)
            {
                Session["User"] = time.User_ID;
                Session["Project"] = time.Project_ID;
				Session["pop"] = true;
				return RedirectToAction("WorkOnProject");
            }

            return PartialView();

        }

		public ActionResult CheckNeedPopup()
		{
			return Json(new { Popup = (bool)(Session["pop"] ?? false) }, JsonRequestBehavior.AllowGet);
		}


}
}
